﻿using JTTD.Domain.Dtos;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using JTTD.Domain.IService;
using Microsoft.Extensions.Configuration;
using JTTD.Domain.Units;
using JTTD.Domain.Dtos.Account.Request;
using JTTD.Extensions;

namespace JTTD.Controllers
{
    /// <summary>
    /// 登录授权
    /// </summary>
    [ApiController]
    [Route("account")]
    public class AccountController : BaseController
    {
        private readonly IConfiguration _configuration;
        private readonly IUserInfoService _userInfoService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="userInfoService"></param>
        public AccountController(IConfiguration configuration, IUserInfoService userInfoService)
        {
            _configuration = configuration;
            _userInfoService = userInfoService;
        }

        /// <summary>
        /// 用户登录(获取token)
        /// </summary>
        /// <returns></returns>
        [HttpPost("authoriz")]
        [EnableCors("any")]
        [UnAuthorize]
        public async Task<ApiResult<string>> GetToken([FromBody]AccountRequestDto account)
        {
            var result = await _userInfoService.UserAccount(account);
            if(!result.IsSuccess)
            {
                return ApiResultUnit.Error<string>(result.Message);
            }
            return await GetIdentityToken(result.Data, _configuration);
        }
    }
}
